package com.rongke.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rongke.mapper.CouponUserMapper;
import com.rongke.model.Coupon;
import com.rongke.model.CouponUser;
import com.rongke.service.CouponUserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @CouponUserServiceImpl
 * @用户优惠券ServiceImpl
 * @version : Ver 1.0
 */
@Service
public class CouponUserServiceImpl extends ServiceImpl<CouponUserMapper, CouponUser> implements CouponUserService {
    @Override
    public List<CouponUser> releaseCouponsForMuser(Long muserId, List<Coupon> coupons, Integer source) {
        List<CouponUser> cus=new ArrayList<>();
        for(Coupon cp:coupons){
            CouponUser cu = new CouponUser();
            cu.setMuserId(muserId);
            cu.setNum(1);
            cu.setCouponId(cp.getId());
            cu.setStatus(1);//可用
            //按天数设置
            if(cp.getValidityDays()!=null){
                Calendar curCal = Calendar.getInstance();
                curCal.setTime(new Date());
                curCal.add(Calendar.DATE,cp.getValidityDays());
                Date validityTime = curCal.getTime();
                cu.setValidityTime(validityTime);
            }else{
                cu.setValidityTime(cp.getExpireTime());
            }
            cu.setSource(source);
            cu.setGmtDatetime(new Date());
            cus.add(cu);
        }
        return cus;
    }
}
